package com.ruoyi.common.utils.ai;

import com.ruoyi.common.exception.ServiceException;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.LicensePlateOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.LicensePlateOCRResponse;
import org.springframework.stereotype.Component;

@Component
public class LicensePlateOCRUtils {

    private final OcrClient client;

    public LicensePlateOCRUtils(OcrClient client) {
        this.client = client;
    }

    public String lpr(String imgUrl){
        LicensePlateOCRRequest licensePlateOCRRequest = new LicensePlateOCRRequest();
        licensePlateOCRRequest.setImageUrl(imgUrl);
        try {
            LicensePlateOCRResponse licensePlateOCRResponse = client.LicensePlateOCR(licensePlateOCRRequest);
            return licensePlateOCRResponse.getNumber();
        } catch (TencentCloudSDKException e) {
            throw new ServiceException("车牌识别失败，腾讯云服务异常："+e.getMessage());
        }
    }

}
